package com.majun.controller;

import com.alibaba.fastjson.JSON;
import com.majun.bean.SysUserDto;
import com.majun.produtor.RocketMQTemplateProducer;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 使用 RocketMQTemplate 发送各种消息
 *
 * @author zy
 * @date 2020/6/9
 */
@Slf4j
@RestController
@RequestMapping("/producer/template")
public class RocketTemplateController {
    @Autowired
    private RocketMQTemplateProducer producer;

    /**
     * 发送普通消息
     */
    @GetMapping("/ordinary")
    public String sendMessage() {
        SysUserDto sysUserDto = SysUserDto.builder()
                .accountName("chen")
                .userName("hua")
                .password("123456")
                .build();
        this.producer.sendMessage("ordinary-message", sysUserDto);
        return "success：消息已发送：message = " + JSON.toJSONString(sysUserDto);
    }

    /**
     * 同步发送消息
     *
     * @return 反馈信息
     */
    @GetMapping("/sync")
    public String syncSendMessage() {
        SysUserDto sysUserDto = SysUserDto.builder()
                .accountName("sync")
                .userName("message")
                .password("12ds456")
                .build();
        this.producer.syncSendMessage("sync-message", sysUserDto);
        return "success：消息已同步发送：message = " + JSON.toJSONString(sysUserDto);
    }

    /**
     * 异步发送消息
     *
     * @return 反馈信息
     */
    @GetMapping("/async")
    public String asyncSendMessage() {
        SysUserDto sysUserDto = SysUserDto.builder()
                .accountName("async")
                .userName("message")
                .password("8752ert")
                .build();
        this.producer.asyncSendMessage("async-message", sysUserDto);
        return "success：消息已异步发送：message = " + JSON.toJSONString(sysUserDto);
    }

    /**
     * 发送批量消息
     *
     * @return 反馈信息
     */
    @GetMapping("/syncMessages")
    public String asyncSendMessages() {
        List<Message<?>> messageList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            messageList.add(MessageBuilder.withPayload(
                    SysUserDto.builder()
                            .id(String.valueOf(i))
                            .accountName("accountName-" + i)
                            .userName("userName-" + i)
                            .password("password-" + i)
                            .email("email-" + i)
                            .build()
            ).build());
        }
        this.producer.syncSendMessages("sync-messages", messageList, 5000);
        return "success：已异步批量发送消息";
    }

    /**
     * 发送事务消息
     *
     * @return 反馈信息
     */
    @GetMapping("/transactionMessage")
    public String sendMessageInTransactionMessage() {
        SysUserDto sysUserDto = SysUserDto.builder()
                .id(String.valueOf(Math.random()))
                .accountName("transaction")
                .userName("message")
                .password("transaction")
                .email("transaction@gmail.com")
                .build();
        this.producer.sendMessageInTransaction("transaction-message", sysUserDto);
        return "success：已发送事务消息：message = " + JSON.toJSONString(sysUserDto);
    }

    /**
     * 单向发送消息
     *
     * @return 反馈信息
     */
    @GetMapping("/oneWay")
    public String oneWaySendMessage() {
        SysUserDto sysUserDto = SysUserDto.builder()
                .accountName("oneWay")
                .userName("message")
                .password("asdew123")
                .build();
        this.producer.sendOneWayMessage("oneWay-message", sysUserDto);
        return "success：消息已单向发送：message = " + JSON.toJSONString(sysUserDto);
    }

    /**
     * 单向发送顺序消息
     *
     * @return 反馈信息
     */
    @GetMapping("/oneWayOrderly")
    public String sendOneWayOrderlyMessage() {
        this.producer.sendOneWayOrderly("oneWay-order-message");
        return "success：已单向发送有序消息.. ";
    }

    /**
     * 同步发送顺序消息
     *
     * @return 反馈信息
     */
    @GetMapping("/syncOrderly")
    public String syncSendOrderlyMessage() {
        this.producer.syncSendOrderly("sync-order-message");
        return "success：已同步发送有序消息.. ";
    }

    /**
     * 同步发送延时消息
     *
     * @return 反馈信息
     */
    @GetMapping("/syncDelay")
    public String syncSendDelayMessage() {
        SysUserDto userDto = SysUserDto.builder()
                .userName("sync")
                .accountName("delay")
                .email("delay.5788@gmail.com")
                .mobile("17898097654")
                .build();
        this.producer.syncSendDelay("sync-delay-message", userDto, 10000, 4);
        return "success：已同步发送延时消息.. ";
    }

    /**
     * 发送携带tag的消息，以便进行消息过滤
     *
     * @return 反馈信息
     */
    @GetMapping("/withTag")
    public String syncSendWithTagMessage() {
        SysUserDto userDto = SysUserDto.builder()
                .userName("tag")
                .accountName("message")
                .email("tag.5788@gmail.com")
                .mobile("17898097654")
                .build();
        this.producer.syncSendMessageWithTag("tag-message", "testTag", userDto);
        return "success：已同步发送带 tag 的消息消息.. ";
    }
}
